#include "lcd.h"

void init_LCD(struct LCD_info *lcdinfo)
{
    static int callme = 0;
    if(callme != 0)
        return;

    lcdinfo->fd = open("/dev/fb0", O_RDWR);
    struct fb_var_screeninfo vinfo;
    ioctl(lcdinfo->fd, FBIOGET_VSCREENINFO, &vinfo);
    lcdinfo->width = vinfo.xres;
    lcdinfo->height= vinfo.yres;
    lcdinfo->bpp   = vinfo.bits_per_pixel;
    lcdinfo->screen_size = lcdinfo->width * lcdinfo->height * lcdinfo->bpp/8 ;

    // 帧缓冲设备(LCD)申请映射内存
    lcdinfo->fb_mem = mmap(NULL, lcdinfo->screen_size,
                           PROT_WRITE, MAP_SHARED, lcdinfo->fd, 0);

    callme++;
}

void relese_LCD(struct LCD_info *lcdinfo)
{
    munmap(lcdinfo->fb_mem, lcdinfo->screen_size);
    close(lcdinfo->fd);
}

void clear_LCD(struct LCD_info *lcdinfo, uint32_t color)
{
    switch(color)
    {
    case LCD_BLACK:
        bzero(lcdinfo->fb_mem, lcdinfo->screen_size);
        break;

    case LCD_WHITE:
        memset(lcdinfo->fb_mem, color, lcdinfo->screen_size);
        break;
    }
}